package com.hsgene.version.controller;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.version.domain.VersionDomain;
import com.hsgene.version.dto.VersionDto;
import com.hsgene.version.exception.VersionErrorCode;
import com.hsgene.version.service.VersionService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 产品更新管理
 * @ProjectName: hdas-geneshop
 * @Package: com.hsgene.version.controller
 * @Author: 江承良
 * @CreateDate: 2019/1/18 15:07
 * @Version: 1.0
 * Copyright: Copyright (c) 2019
 */
@RestController
@RequestMapping("/v1.0/genetic/testing")
public class VersionController {
    @Resource
    private VersionService versionService;

    /**
     * @param
     * @return com.hsgene.common.util.result.ApiResult<java.util.List<java.lang.String>>
     * @description 获取所有版本号
     * @author maodi
     * @createDate 2019/1/21 10:36
     */
    @GetMapping("/versions/nos")
    public ApiResult<List<String>> getVersionsNos() {
        return ApiResult.succ(versionService.getVersionsNos());
    }

    /**
     * @param versionNo 版本号
     * @return com.hsgene.common.util.result.ApiResult<com.hsgene.version.dto.VersionDto>
     * @description 根据版本号查询日志版本信息
     * @author maodi
     * @createDate 2019/1/21 10:26
     */
    @GetMapping("/versions/logs")
    public ApiResult<List<VersionDto>> getVersionsLogs(@RequestParam("versionNo") String versionNo) {
        if (StringUtils.isBlank(versionNo)) {
            return ApiResult.fail(VersionErrorCode.VERSION_NO_IS_NOT_NULL);
        }
        return ApiResult.succ(versionService.getVersionsLogs(versionNo));
    }

    /***
     * 添加产品更新内容
     * @author jcl
     * @date 2019/1/18 10:12
     * @param versionDomain
     * @return
     */
    @PostMapping("/version")
    public ApiResult addVersion(@RequestBody VersionDomain versionDomain) {
        return versionService.addVersion(versionDomain);
    }

    /***
     * 产品更新列表查询
     * @author jcl
     * @date 2019/1/18 14:12
     * @param page 页码
     * @param size 大小
     * @param versionNo 版本号
     * @param content 内容
     * @param isRelease 是否包含发布时间
     * @return
     */
    @GetMapping("/version")
    public ApiResult versionPages(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                  @RequestParam(name = "size", defaultValue = "15") Integer size,
                                  @RequestParam(name = "versionNo", required = false) String versionNo,
                                  @RequestParam(name = "content", required = false) String content,
                                  @RequestParam(name = "orderField", defaultValue = "version_no") String orderField,
                                  @RequestParam(name = "isRelease", defaultValue = "false") Boolean isRelease) {
        LimitHelper limitHelper = LimitHelper.create(page, size, true);
        Date releaseDateTime = null;
        if (isRelease) {
            releaseDateTime = new Date();
        }
        return versionService.versionPages(limitHelper, versionNo, content, orderField, releaseDateTime);
    }

    /***
     * 根据id查询产品更新详情
     * @author jcl
     * @date 2019/1/18 14:18
     * @param id
     * @return
     */
    @GetMapping("/version/{id}")
    public ApiResult versionById(@PathVariable("id") String id) {
        return versionService.versionById(id);
    }

    /***
     * 更新操作
     * @author jcl
     * @date 2019/1/18 15:59
     * @param versionDomain
     * @return
     */
    @PutMapping("/version")
    public ApiResult updateVersion(@RequestBody VersionDomain versionDomain) {
        return versionService.updateVersion(versionDomain);
    }

    /***
     * 删除
     * @author jcl
     * @date 2019/1/18 16:08
     * @param id
     * @return
     */
    @DeleteMapping("/version/{id}")
    public ApiResult deleteVersion(@PathVariable("id") String id) {
        return versionService.deleteVersion(id);
    }

    /***
     * 根据版本号查询产品更新详情
     * @author jcl
     * @date 2019/1/18 14:18
     * @param versionNo
     * @return
     */
    @GetMapping("/version/version_no/{versionNo}")
    public ApiResult versionByVersionNo(@PathVariable("versionNo") String versionNo) {
        return versionService.versionByVersionNo(versionNo);
    }

    /***
     * 检查版本号是否重复
     * @author jcl
     * @date 2019/1/29 10:18
     * @param versionNo
     * @param versionId
     * @return
     */
    @GetMapping("/version/version_no_repeat/{versionNo}")
    public ApiResult versionNoRepeat(@PathVariable("versionNo")String versionNo,String versionId){
        return versionService.versionNoRepeat(versionNo,versionId);
    }
}
